﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CIT.WMS.Transefer
{
    public partial class FrmTranseferOut : Form
    {
        public FrmTranseferOut()
        {
            InitializeComponent();
        }

        DataTable DtOut;

        decimal Qty = 0;
        string realLocationSN;

        private void cmb_BillNo_SelectedIndexChanged(object sender, EventArgs e)
        {
            InputBillNo();
        }

        private void cmb_BillNo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 13)
            {
                return;
            }

            InputBillNo();
        }


        private void txt_Qty_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 13)
            {
                return;
            }

            if (string.IsNullOrEmpty(txt_Qty.Text))
            {
                MessageBox.Show("请输入下架数量！");
                return;
            }

            if (string.IsNullOrEmpty(txt_LocationSN.Text))
            {
                MessageBox.Show("下架的库位不能为空!");
                return;
            }

            var qty = Convert.ToDecimal(0);

            try
            {
                qty = Convert.ToDecimal(txt_Qty.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show("输入的下架数量格式不对，请重新输入!");
                txt_Qty.Text = "";
                return;
            }

            var inventory = Bll_Transefer.GetInventoryInfo(realLocationSN);

            if (inventory.Rows.Count == 0)
            {
                MessageBox.Show(realLocationSN + "库位库存不足!");
                return;
            }

            var inv_Qty = Convert.ToDecimal(inventory.Rows[0]["Qty"]);

            if (qty > inv_Qty)
            {
                MessageBox.Show("下架数量" + qty + "大于库存数量" + inv_Qty + ";操作失败!");
                txt_Qty.Text = "";
                return;
            }


            MessageBoxButtons messButton = MessageBoxButtons.OKCancel;

            DialogResult dr = MessageBox.Show("本次操作的下架数量：" + qty + ", 请确认！", "下架确认", messButton, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);

            if (dr == DialogResult.OK)
            {
                Qty = qty;

                //var isSuccess = Bll_Transefer.TransferOut(cmb_BillNo.Text, realLocationSN, Qty);
                var isSuccess = Utils.NMS.sqlDBService.TransferOut(cmb_BillNo.Text, realLocationSN, Qty, Utils.uContext.UserNamek__BackingField);
                if (isSuccess)
                {
                    lab_msg.Text = cmb_BillNo.Text + "下架成功!\r\n请选择其他单号！";

                    BindCMB();
                    cmb_BillNo.Focus();
                    txt_LocationSN.Text = "";
                    txt_Qty.Text = "";

                }
                else
                {
                    MessageBox.Show(cmb_BillNo.Text + "下架失败!\r\n请检查此库位库存是否足够！");
                }
            }
        }

        public void BindCMB()
        {
            DtOut = Bll_Transefer.GetTransferDT("0");

            cmb_BillNo.Text = "";
            cmb_BillNo.Items.Clear();

            if (DtOut.Rows.Count > 0)
            {
                foreach (DataRow dr in DtOut.Rows)
                {
                    cmb_BillNo.Items.Add(dr["BillNo"].ToString());
                }
            }
        }

        public void InputBillNo()
        {
            DataRow[] drOut = DtOut.Select(string.Format(" BillNo = '{0}'", cmb_BillNo.Text));
            if (drOut.Length > 0)
            {

                DataTable info = Bll_Transefer.GetTransferInfo(cmb_BillNo.Text);
                if (info.Rows.Count > 0)
                {
                    //if (string.IsNullOrEmpty(info.Rows[0]["Updator"].ToString()) || Utils.uContext.UserNamek__BackingField.Equals(info.Rows[0]["Updator"].ToString()))
                    //{
                    realLocationSN = info.Rows[0]["OutLocation_SN"].ToString();


                    try
                    {
                        txt_LocationSN.Text = realLocationSN.Split('#')[1] + realLocationSN.Split('#')[2];
                        txt_Qty.Focus();

                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("库位" + realLocationSN + "格式不正确！");
                        return;
                    }

                    var inventory = Bll_Transefer.GetInventoryInfo(realLocationSN);


                    var inv_Qty = Convert.ToDecimal(0);

                    if (inventory.Rows.Count > 0)
                    {
                        inv_Qty = Convert.ToDecimal(inventory.Rows[0]["Qty"]);

                        if (info.Rows[0]["PartNumber"].ToString() != inventory.Rows[0]["PartNumber"].ToString())
                        {
                            MessageBox.Show("移库物料" + info.Rows[0]["PartNumber"].ToString() + "与库位物料" + inventory.Rows[0]["PartNumber"].ToString() + "不一致！");
                            return;
                        }
                    }



                    lab_msg.Text = "仓库:" + info.Rows[0]["OutStorage_SN"].ToString() + "\r\n物料号:" + info.Rows[0]["PartNumber"].ToString()
                        + "\r\n库存总数:" + inv_Qty;
                    //}
                    //else
                    //{
                    //    MessageBox.Show(cmb_BillNo.Text + "已被" + info.Rows[0]["Updator"].ToString() + "操作，请选择其他单号!");

                    //    cmb_BillNo.Text = "";
                    //    txt_LocationSN.Text = "";
                    //    txt_Qty.Text = "";
                    //}
                }
            }
            else
            {
                //lab_msg.Text = cbx_Doc.Text + "单号不正确,请输入正确的单据!";
                MessageBox.Show(cmb_BillNo.Text + "单号不正确,请输入正确的单据!");
                cmb_BillNo.Text = "";
                cmb_BillNo.Focus();

            }
        }

        private void FrmTranseferOut_Load(object sender, EventArgs e)
        {
            BindCMB();
        }

    }
}